//
//  SetRateViewController.m
//  ActSistant
//
//  Created by Hieu Nguyen Trung on 3/6/13.
//  Copyright (c) 2013 Savvycom. All rights reserved.
//

#import "SetRateViewController.h"
#import <QuartzCore/QuartzCore.h>
#import "SetRateCell.h"


@interface SetRateViewController ()

@end

@implementation SetRateViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

-(id) initWithScreenType:(SelectScreenType)type{
    self = [super init];
    if (self){
        screenType = type;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self initializeViews];
}

-(void) initializeViews{
    [self.navigationView.btnBack addTarget:self action:@selector(btnBackClicked) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationView.btnRight1 addTarget:self action:@selector(btnSettingClicked) forControlEvents:UIControlEventTouchUpInside];
    
//    if (screenType == SelectAllowancesScreen){
//        [self.lblTitle setText:@"Select Allowances"];
//    }
    
    self.tableView.tableFooterView = [[[UIView alloc] initWithFrame:CGRectZero] autorelease];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_navigationView release];
    [_tableView release];
    [_lblTitle release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setNavigationView:nil];
    [self setTableView:nil];
    [self setLblTitle:nil];
    [super viewDidUnload];
}

#pragma mark - Action Handle
-(void) btnBackClicked{
    [self.navigationController popViewControllerAnimated:true];
}

-(void) btnSettingClicked{
    ActSettingViewController *settingVC = [[[ActSettingViewController alloc] init] autorelease];
    [self.navigationController pushViewController:settingVC animated:true];
}

#pragma mark - UITableView Methods Implementation
-(int) numberOfSectionsInTableView:(UITableView *)tableView{
    if (screenType == SelectRateScreen){
        return [RATE_TYPE count];
    }
    return 1;
}

-(int) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (screenType == SelectRateScreen){
        switch (section) {
            case 0:
                return [THEATRICAL_LABELS_STRING_ARRAY count];
                break;
            case 1:
                return [TELEVISION_LABELS_STRING_ARRAY count];
                break;
            default:
                return [FEATURE_LABELS_STRING_ARRAY count];
                break;
        }
    }
    return 3;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (screenType == SelectRateScreen){
        return 44;
    }
    return 0;
}

-(CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(void) scrollViewDidScroll:(UIScrollView *)scrollView{
    CGFloat sectionHeaderHeight = 44;
    if (scrollView.contentOffset.y <= sectionHeaderHeight&& scrollView.contentOffset.y >= 0) {
        scrollView.contentInset = UIEdgeInsetsMake(-scrollView.contentOffset.y, 0, 0, 0);
    }
    else if (scrollView.contentOffset.y>=sectionHeaderHeight) {
        scrollView.contentInset = UIEdgeInsetsMake(-sectionHeaderHeight, 0, 0, 0);
    }
}

-(UIView *) tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    
    UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
    if (screenType == SelectRateScreen){
        [label setText:RATE_TYPE[section]];
        [label setTextColor:[UIColor darkGrayColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        
        UIImageView *imgView = [[UIImageView alloc] initWithImage:[Util imageWithColor:[UIColor colorWithRed:224/255.0 green:224/255.0 blue:224/255.0 alpha:1]]];
        [imgView setFrame:CGRectMake(0, 43, 320, 1)];
        
        [view addSubview:imgView];
    }
    else{
        [label setText:@"Select Allowances"];
        [label setTextColor:[UIColor whiteColor]];
        [label setTextAlignment:NSTextAlignmentCenter];
        [label setBackgroundColor:[UIColor clearColor]];
        [view setBackgroundColor:[UIColor colorWithRed:1 green:86/255.0 blue:86/255.0 alpha:1]];
    }
    
    [view addSubview:label];
    return view;
}

-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell;
    
    NSArray *objectsInXIB = [[NSBundle mainBundle] loadNibNamed:@"PayrollAgencyCell" owner:nil options:nil];
    cell = objectsInXIB[2];
    if (screenType == SelectRateScreen){
        switch (indexPath.section) {
            case 0:
                [((SetRateCell *) cell).lblTitle setText:THEATRICAL_LABELS_STRING_ARRAY[indexPath.row]];
                break;
            case 1:
                [((SetRateCell *) cell).lblTitle setText:TELEVISION_LABELS_STRING_ARRAY[indexPath.row]];
                break;
            case 2:
                [((SetRateCell *) cell).lblTitle setText:FEATURE_LABELS_STRING_ARRAY[indexPath.row]];
                break;
            default:
                break;
        }
    }
    else{
        switch (indexPath.row) {
            case 0:
                [((SetRateCell *) cell).lblTitle setText:@"Theatrical"];
                [((SetRateCell *) cell) setSelectedTitleColor:[UIColor lightGrayColor]];
                break;
            case 1:
                [((SetRateCell *) cell).lblTitle setText:@"Television"];
                [((SetRateCell *) cell) setSelectedTitleColor:[UIColor lightGrayColor]];
                break;
            case 2:
                [((SetRateCell *) cell).lblTitle setText:@"Feature"];
                [((SetRateCell *) cell) setSelectedTitleColor:[UIColor lightGrayColor]];
                UIView *cellBackgroundView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 44)] autorelease];
                [cellBackgroundView setBackgroundColor:[UIColor whiteColor]];
                [((SetRateCell *) cell) setSelectedBackgroundView:cellBackgroundView];
                break;
            default:
                break;
        }
    }
    
    return cell;
}
@end
